<template>
  <div>
    <el-form ref="dataForm" :rules="rules" :model="form" label-position="right" label-width="100px" class="form-style">
      <el-form-item label="Name" prop="name">
        <el-input v-model="form.name" clearable maxlength="150" show-word-limit />
        <el-input-number v-model="num" :min="1" :max="1000" />
        <el-button type="primary" icon="el-icon-setting" plain @click="num=0" />
        <text-copy :value="num+'-'" />
      </el-form-item>
      <el-form-item label="m3u8Path" prop="m3u8Path">
        <el-input v-model="form.m3u8Path" clearable maxlength="255" show-word-limit />
      </el-form-item>
      <el-form-item label="keyPath" prop="keyPath">
        <el-input v-model="form.keyPath" clearable maxlength="255" show-word-limit />
      </el-form-item>
      <el-form-item label="headUrl" prop="headUrl">
        <el-input v-model="form.headUrl" clearable maxlength="255" show-word-limit />
      </el-form-item>
      <el-form-item label="origin" prop="origin">
        <el-input v-model="form.origin" clearable maxlength="255" show-word-limit />
      </el-form-item>
      <el-form-item label="state">
        <el-input v-model="form.state" :autosize="{ minRows: 3, maxRows: 5 }" type="textarea" placeholder="Please input" />
      </el-form-item>
      <el-form-item>
        <el-button v-show="!isEdit" type="text" @click="reset">Reset</el-button>
        <el-button v-waves v-loading="loading" type="primary" @click="submitForm">Save Now</el-button>
      </el-form-item>
      <el-form-item label="savePath" prop="savePath">
        <el-input v-model="form.savePath" clearable maxlength="255" show-word-limit />
      </el-form-item>
      <el-form-item label="tmpPath" prop="tmpPath">
        <el-input v-model="form.tmpPath" clearable maxlength="255" show-word-limit />
      </el-form-item>
      <el-form-item label="completed" prop="completed">
        <el-switch v-model="form.completed" />
      </el-form-item>
      <el-form-item label="synthesized" prop="synthesized">
        <el-switch v-model="form.synthesized" />
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { createOrUpdateTsData } from '@/api/util/ts-data'
import waves from '@/directive/waves' // waves directive
import TextCopy from '@/views/components/TextCopy.vue'

const defaultForm = {
  name: '',
  m3u8Path: '',
  keyPath: '',
  headUrl: '',
  origin: '',
  savePath: '',
  tmpPath: '',
  state: '',
  completed: false,
  synthesized: false,
  uid: ''
}

export default {
  name: 'TSData',
  components: { TextCopy },
  directives: { waves },
  props: {
    isEdit: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      form: {},
      rules: {
        name: [{ required: true, message: 'name is required', trigger: 'blur' }],
        m3u8Path: [{ required: true, message: 'm3u8Path is required', trigger: 'blur' }],
        keyPath: [{ required: true, message: 'keyPath is required', trigger: 'blur' }]
      },
      loading: false,
      num: 1
    }
  },
  computed: {
    ...mapGetters(['uid'])
  },
  created() {
    if (this.isEdit) {
      const row = this.$route.params
      this.form = Object.assign({}, row, { modifyAt: undefined })
    } else {
      this.form = Object.assign({}, defaultForm, { uid: this.uid })
    }
  },
  methods: {
    submitForm() {
      console.log(this.form)
      this.$refs.dataForm.validate(valid => {
        if (valid) {
          if (this.form.m3u8Path.indexOf('m3u8') === -1) {
            this.$message('m3u8Path incorrectness')
            return
          }
          this.loading = true
          createOrUpdateTsData(this.form)
            .then(response => {
              this.$notify({
                title: 'Success',
                message: 'save successfully',
                type: 'success',
                duration: 2000
              })
              this.num += 1
              if (!this.isEdit) {
                this.reset()
              }
            })
            .catch(() => {
              this.loading = false
            })
          this.loading = false
        } else {
          return false
        }
      })
    },
    reset() {
      this.$refs['dataForm'].resetFields()
    }
  }
}
</script>

<style lang="scss" scoped>
.form-style {
  margin-top: 1%;
  width: 88%;
  margin-left: 6%;
}
</style>
